<template>
	<view class="current">
		<!-- <view class="ful f-row ai-c jc-sb">
			<view class="ful-left">
				头像
			</view>
			<view class="ful-right">
				<u-upload :fileList="fileList1" name="1" :maxCount="1" @afterRead="afterRead" @delete="deletePic">
					<image :src="data.imgSrc" mode=""></image>
				</u-upload>
			</view>
		</view> -->
		<view class="ful f-row ai-c jc-sb">
			<view class="ful-left">
				旧密码
			</view>
			<view class="ful-right">
				<u--input placeholder="请输入旧密码" border="none" v-model="data.old_password" class=""
					:password="typeOldPassWord">
				</u--input>
				<view class="" style="margin-left: 10rpx;">
					<u-icon size="18" name="eye-fill" @click="seePassWord(0)"></u-icon>
				</view>

			</view>
		</view>
		<view class="ful f-row ai-c jc-sb">
			<view class="ful-left">
				新密码
			</view>
			<view class="ful-right">
				<u--input placeholder="请输入旧密码" border="none" v-model="data.password" class="" :password="typePassWord">
				</u--input>
				<view class="" style="margin-left: 10rpx;">
					<u-icon size="18" name="eye-fill" @click="seePassWord(1)" style="margin-left: 10rpx;"></u-icon>
				</view>
			</view>
		</view>
		<view class="ful f-row ai-c jc-sb">
			<view class="ful-left">
				确认新密码
			</view>
			<view class="ful-right">
				<u--input placeholder="请输入旧密码" border="none" v-model="aginPassword" class=""
					:password="typeAginPassword">
				</u--input>
				<view class="" style="margin-left: 10rpx;">
					<u-icon size="18" name="eye-fill" @click="seePassWord(2)" style="margin-left: 10rpx;"></u-icon>
				</view>
			</view>
		</view>
		<view class="f-row " style="justify-content: center;">
			<view class="alterInfo">
				<u-button text="修改密码" type='primary' @click="submit"
					customStyle="background: linear-gradient(90deg, #2cb9a4 0%, #C7EACE 100%);color:#fff;margin-top:20px;border-radius: 30px;border:none;"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import eventBUs from '../../utils/eventBus.js'
	import {
		resetpwd
	} from '@/request/api.js'
	export default {
		data() {
			return {
				data: {
					old_password: '',
					password: '',
				},
				aginPassword: '',
				typeOldPassWord: true,
				typePassWord: true,
				typeAginPassword: true,
				fileList1: []
			}
		},
		onLoad() {
			// this.getUser()
			// this.getUserInfo()
		},
		methods: {
			seePassWord(index) {
				switch (index) {
					case 0:
						this.typeOldPassWord = !this.typeOldPassWord
						break;
					case 1:
						this.typePassWord = !this.typePassWord
						break;
					case 2:
						this.typeAginPassword = !this.typeAginPassword
						break;
				}
			},
			async getUserInfo() {
				let res = await getUser()
				uni.setStorageSync('userInfo', res.data)
				this.$eventBus.$emit('hasLogin', true);
				console.log(res)
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			getUser() {
				let data = uni.getStorageSync('userInfo')
				// this.fileList1.push({url:data.avatar})
				this.data.nickname = data.nickname
				this.data.imgSrc = data.avatar
				// console.log(this.data)
			},
			submit() {
				if (this.data.old_password == '') {
					uni.showToast({
						title: '请填写旧密码',
						icon: 'none',
						duration: 1500
					})
					return false
				}
				if (this.data.password == '') {
					uni.showToast({
						title: '请填写新密码',
						icon: 'none',
						duration: 1500
					})
					return false
				}
				if (this.aginPassword == '') {
					uni.showToast({
						title: '请填写确认密码',
						icon: 'none',
						duration: 1500
					})
					return false
				}
				if (this.aginPassword !== this.data.password) {
					uni.showToast({
						title: '确认密码与输入密码不一致',
						icon: 'none',
						duration: 1500
					})
					return false
				}

				resetpwd(this.data).then(res => {
					if (res.code == 200) {
						uni.navigateTo({
							url: '/my_pages/components/success?title=修改个人密码成功'
						})
					}
				})
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				const token = uni.getStorageSync('shopToken')
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${getApp().globalData.baseUrl}/api/common/upload`, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							token
						},
						success: (res) => {
							setTimeout(() => {
								console.log(res)
								resolve(JSON.parse(res.data).data.fullurl)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.current {
		padding: 30upx;
		padding-top: 0;
	}

	.ful-left {
		width: 25%;
	}

	.ful {
		font-size: 28upx;
		color: #4F4F4F;
		padding: 40upx 0;
		border-bottom: 1upx solid #ECECEC;
	}

	.ful-right {
		display: flex;
		align-items: center;
		image {
			width: 100upx;
			height: 100upx;
			border-radius: 50%;
		}
	}

	.alterInfo {
		width: 95%;
		margin-top: 50%;
	}

	::v-deep .u-input__content__field-wrapper__field {
		text-align: right !important;
	}
</style>